#include "PageLib.h"

PageLib::PageLib()
    : _dirScanner(), _filePro(), _pages(), _offsetLib() {
    // 初始化扫描和处理工具
}

void PageLib::create() {
    // 创建网页库和网页索引库
    string path = "/home/kyle/Project/WebDisk/data/PeopleWeb";
    _dirScanner.traverse(path);
    vector<string> files = _dirScanner.getFiles();

    size_t start_pos = 0;
    size_t start_id = 1;

    for (auto file : files) {
        cout << "current id = " << start_id << "\n";
        cout << "current path= " << file << "\n";
        // 清空数据，防止溢出
        _pages.clear();
        _offsetLib.clear();

        // 返回一个rss结构体
        vector<RssItem> rss = _filePro.process(file);
        if (rss.size() == 0) {
            continue;
        }

        for (auto &rs : rss) {
            // 格式化
            stringstream ss_rss;
            ss_rss << "<doc>" << "\n";
            ss_rss << "\t<docid>" << start_id << "</docid>" << "\n";
            ss_rss << "\t<title>" << rs.title << "</title>" << "\n";
            ss_rss << "\t<link>" << rs.link << "</link>" << "\n";
            ss_rss << "\t<content>" << rs.description << "</content>" << "\n";
            ss_rss << "</doc>";

            // 保存为string，更新网页库信息
            string str_rss = ss_rss.str();
            _pages.push_back(str_rss);

            // 更新网页偏移库
            size_t curr_len = str_rss.size();
            _offsetLib[start_id] = std::make_pair(start_pos, curr_len);
            start_pos += curr_len; // 更新开始位置
            // cout << "docid = " << start_id << "\n";
            ++start_id; // 更新文章id
        }
        // 每个文件都进行保存
        store();
    }
}

void PageLib::store() {
    // 存储处理好的网页库和偏移库
    string ripepage = "/home/kyle/Project/WebDisk/data/ripepage.dat";
    string offset = "/home/kyle/Project/WebDisk/data/offset.dat";

    ofstream ofs_ripe(ripepage, std::ios::app);
    ofstream ofs_off(offset, std::ios::app);

    for (auto &page : _pages) {
        ofs_ripe << page;
    }

    for (auto &pair : _offsetLib) {
        ofs_off << pair.first << " "
                << pair.second.first << " "
                << pair.second.second << "\n";
    }

    ofs_ripe.close();
    ofs_off.close();
}
